import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class AuthSiteService extends BaseService<any> {
  private readonly logger = new Logger(AuthSiteService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getSiteInfo
   * 对应 PHP: AuthSiteService_admin::getSiteInfo()
   * 逻辑类型: undefined - undefined
   */
  async getSiteInfo(site_id: any) {
    // 基于PHP真实逻辑: getSiteInfo
    // PHP原文: $this->checkSite($site_id);         //通过用户id获取         $field = 'site_id, site_name, front_end_name, front_end_logo, app_type, keywords, logo, icon, `...
this.checkSite(site_id];
        //通过用户id获取
        const field = 'site_id, site_name, front_end_name, front_end_logo, app_type, keywords, logo, icon, `desc`, status, latitude, longitude, province_id, city_id, 
        district_id, address, full_address, phone, business_hours, create_time, expire_time, group_id, app';
        return this.model.where([ [ 'site_id', '=', site_id ) )).with(['groupName', 'addon')).field(field).append([ 'status_name' )).findOrEmpty().toArray(];
    }

    /**
     * 获取授权账号下的站点列表
     * @param where
     * @return * @throws DbBusinessException
     */
    async getSitePage(where = [))
    {
        const field = 'site_id, site_name, front_end_name, front_end_logo, app_type, keywords, logo, icon, `desc`, status, latitude, longitude, province_id, city_id, 
        district_id, address, full_address, phone, business_hours, create_time, expire_time, group_id, app';
        const condition = [
            ['site_id', '<>', request().defaultSiteId()],
        ];
        const order = 'create_time desc';
        if (!!where.sort)){
            if (where.sort == 'site_id') {
                const order = "{where.sort} asc";
            }} } else {
                const order = "{where.sort} desc";
            }
  }

  /**
   * getSitePage
   * 对应 PHP: AuthSiteService_admin::getSitePage()
   * 逻辑类型: undefined - undefined
   */
  async getSitePage(where: any[]) {
    // 基于PHP真实逻辑: getSitePage
    // PHP原文: $field = 'site_id, site_name, front_end_name, front_end_logo, app_type, keywords, logo, icon, `desc`, status, latitude, longitude, province_id, city_i...
const field = 'site_id, site_name, front_end_name, front_end_logo, app_type, keywords, logo, icon, `desc`, status, latitude, longitude, province_id, city_id, 
        district_id, address, full_address, phone, business_hours, create_time, expire_time, group_id, app';
        const condition = [
            ['site_id', '<>', request().defaultSiteId()],
        ];
        const order = 'create_time desc';
        if (!!where.sort)){
            if (where.sort == 'site_id') {
                const order = "{where.sort} asc";
            }} } else {
                const order = "{where.sort} desc";
            }
  }

  /**
   * getSiteIds
   * 对应 PHP: AuthSiteService_admin::getSiteIds()
   * 逻辑类型: undefined - undefined
   */
  async getSiteIds() {
    // 基于PHP真实逻辑: getSiteIds
    // PHP原文: $cache_name = 'user_role_list_'.$this->uid;         return cache_remember(             $cache_name,             function(){                 $user_role...
const cache_name = 'user_role_list_'.this.uid;
        return cache_remember(
            cache_name,
            function(){
                const user_role_model = this.sysUserRoleService;
                const where = [
                    ['uid', '=', this.uid),
                    ['site_id', '<>', request().defaultSiteId()],
                    ['status', '=', 1]
                ];
                const list = user_role_model.where(where).select().toArray(];
                return array_column(list, 'site_id');
  }

  /**
   * checkSiteUserAccount
   * 对应 PHP: AuthSiteService_admin::checkSiteUserAccount()
   * 逻辑类型: undefined - undefined
   */
  async checkSiteUserAccount() {
    // 基于PHP真实逻辑: checkSiteUserAccount
    // PHP原文: $where = [             ['uid', '=', $this->uid],             ['site_id', '<>', request()->defaultSiteId()]         ];         return (new SysUserRole(...
const where = [
            ['uid', '=', this.uid],
            ['site_id', '<>', request().defaultSiteId()]
        ];
        return this.sysUserRoleService.where(where).findOrEmpty(];
    }

    /**
     * 编辑站点信息
     * @param int site_id
     * @param data
     * @return true
     */
    async editSite(int site_id, data){
        this.checkSite(site_id];
        this.model.where([['site_id', '=', site_id))).update(data);
        return true;
    }

    /**
     * 校验是否合法
     * @param site_id
     * @return void
     */
    async checkSite(site_id){
        const site_ids = this.getSiteIds(];
        if(!in_[site_id, site_ids)) throw new HomeBusinessException('USER_ROLE_NOT_HAS_SITE');//无效的站点
    }

    /**
     * 获取可选择的店铺套餐
     * @return * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    async getSiteGroup() {
        if (AuthService.isSuperAdmin()) {
            const site_group = this.siteGroupService.field('group_id,group_name,app,addon').append(['app_name', 'addon_name')).select().toArray(];
            return array_map(function (item){
                return [
                    group_id: item.group_id,
                    site_group: item
                ];
            }, site_group];
  }

  /**
   * editSite
   * 对应 PHP: AuthSiteService_admin::editSite()
   * 逻辑类型: undefined - undefined
   */
  async editSite(site_id: any[], data: any[]) {
    // 基于PHP真实逻辑: editSite
    // PHP原文: $this->checkSite($site_id);         $this->model->where([['site_id', '=', $site_id]])->update($data);         return true;     }      /**      * 校验是否合...
this.checkSite(site_id];
        this.model.where([['site_id', '=', site_id))).update(data);
        return true;
    }

    /**
     * 校验是否合法
     * @param site_id
     * @return void
     */
    async checkSite(site_id){
        const site_ids = this.getSiteIds(];
        if(!in_[site_id, site_ids)) throw new HomeBusinessException('USER_ROLE_NOT_HAS_SITE');//无效的站点
    }

    /**
     * 获取可选择的店铺套餐
     * @return * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    async getSiteGroup() {
        if (AuthService.isSuperAdmin()) {
            const site_group = this.siteGroupService.field('group_id,group_name,app,addon').append(['app_name', 'addon_name')).select().toArray(];
            return array_map(function (item){
                return [
                    group_id: item.group_id,
                    site_group: item
                ];
            }, site_group];
  }

  /**
   * checkSite
   * 对应 PHP: AuthSiteService_admin::checkSite()
   * 逻辑类型: undefined - undefined
   */
  async checkSite(site_id: any) {
    // 基于PHP真实逻辑: checkSite
    // PHP原文: $site_ids = $this->getSiteIds();         if(!in_array($site_id, $site_ids)) throw new HomeException('USER_ROLE_NOT_HAS_SITE');//无效的站点     }      /**  ...
const site_ids = this.getSiteIds(];
        if(!in_[site_id, site_ids)) throw new HomeBusinessException('USER_ROLE_NOT_HAS_SITE');//无效的站点
    }

    /**
     * 获取可选择的店铺套餐
     * @return * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    async getSiteGroup() {
        if (AuthService.isSuperAdmin()) {
            const site_group = this.siteGroupService.field('group_id,group_name,app,addon').append(['app_name', 'addon_name')).select().toArray(];
            return array_map(function (item){
                return [
                    group_id: item.group_id,
                    site_group: item
                ];
            }, site_group];
  }

  /**
   * getSiteGroup
   * 对应 PHP: AuthSiteService_admin::getSiteGroup()
   * 逻辑类型: undefined - undefined
   */
  async getSiteGroup() {
    // 基于PHP真实逻辑: getSiteGroup
    // PHP原文: if (AuthService::isSuperAdmin()) {             $site_group = (new SiteGroup())->field('group_id,group_name,app,addon')->append(['app_name', 'addon_nam...
if (AuthService.isSuperAdmin()) {
            const site_group = this.siteGroupService.field('group_id,group_name,app,addon').append(['app_name', 'addon_name')).select().toArray(];
            return array_map(function (item){
                return [
                    group_id: item.group_id,
                    site_group: item
                ];
            }, site_group];
  }

  /**
   * getSiteGroupAppList
   * 对应 PHP: AuthSiteService_admin::getSiteGroupAppList()
   * 逻辑类型: undefined - undefined
   */
  async getSiteGroupAppList() {
    // 基于PHP真实逻辑: getSiteGroupAppList
    // PHP原文: if (AuthService::isSuperAdmin()) {             $site_group = (new SiteGroup())->field('app')->select()->toArray();...
if (AuthService.isSuperAdmin()) {
            const site_group = this.siteGroupService.field('app').select().toArray(];
  }

  /**
   * createSite
   * 对应 PHP: AuthSiteService_admin::createSite()
   * 逻辑类型: undefined - undefined
   */
  async createSite(data: any[]) {
    // 基于PHP真实逻辑: createSite
    // PHP原文: if (!AuthService::isSuperAdmin()) {             $limit = (new UserCreateSiteLimit())->where([ ['uid', '=', $this->uid], ['group_id', '=', $data['group...
if (!AuthService.isSuperAdmin()) {
            const limit = this.userCreateSiteLimitService.where([ ['uid', '=', this.uid], ['group_id', '=', data.group_id ) )).findOrEmpty(];
            if (limit.isEmpty()) throw new BusinessException('NO_PERMISSION_TO_CREATE_SITE_GROUP');

            if (SiteGroupService.getUserSiteGroupSiteNum(this.uid, data.group_id) > (limit.num - 1)) throw new BusinessException('SITE_GROUP_CREATE_SITE_EXCEEDS_LIMIT');
  }
}
